package ee.roosideSoda.backend.model.dao.postgresqlimplementations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;

import ee.roosideSoda.backend.database.ConnectionManager;
import ee.roosideSoda.backend.model.dao.KaimasolevaYldkysimuseAndmedDAO;
import ee.roosideSoda.backend.model.data.KaimasolevaYldkysimuseAndmed;

public class KaimasolevaYldkysimuseAndmedDAOPostgresqlImpl implements KaimasolevaYldkysimuseAndmedDAO 
{
	Logger logger = Logger.getLogger(KaimasolevaYldkysimuseAndmedDAOPostgresqlImpl.class);
	Connection dbConnection;
	String sql;
	PreparedStatement preparedStatement;
	Statement statement;
	ResultSet resultSet;
	private final int WARNING_NO_ROWS_AFFECTED = 0;
	private int rowsAffected;
	
	public KaimasolevaYldkysimuseAndmedDAOPostgresqlImpl(Connection dbConnection)
	{
		this.dbConnection = dbConnection;
	}

	@Override
	public KaimasolevaYldkysimuseAndmed saaKaimasolevaYldkysimuseAndmed() 
	{
		resultSet = null;
		KaimasolevaYldkysimuseAndmed kaimasolevaYldkysimuseAndmedAndmebaasist = null;
		sql = "SELECT * FROM KaimasolevaKysimuseAndmed WHERE id = ?";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(
						sql,
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_READ_ONLY
					);
			//filling the placeholders 
			preparedStatement.setInt(1, 0);
			logger.info(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			//place the cursor at the beginning of a received data set (so it can start to read and process data)
			resultSet.beforeFirst();
			
			if (resultSet.next())
			{
				//setObject() is used, when column or field value in java, can also be null.
				kaimasolevaYldkysimuseAndmedAndmebaasist = new KaimasolevaYldkysimuseAndmed();
				kaimasolevaYldkysimuseAndmedAndmebaasist.setId(resultSet.getInt("id"));
				kaimasolevaYldkysimuseAndmedAndmebaasist.setKaimasolevaKysimuseID(resultSet.getInt("kaimasolevaKysimuseID"));
				kaimasolevaYldkysimuseAndmedAndmebaasist.setKasLuuaUuedTiimid(resultSet.getBoolean("kasLuuaUuedTiimid"));
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return kaimasolevaYldkysimuseAndmedAndmebaasist;
	}

	@Override
	public int uuendaKaimasolevaYldkysimuseAndmeid(
			KaimasolevaYldkysimuseAndmed kaimasolevaYldkysimuseAndmed)
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "UPDATE KaimasolevaKysimuseAndmed " + 
				"SET kaimasolevaKysimuseID = ?, kasLuuaUuedTiimid = ? " + 
				"WHERE id = ?;"; 
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql);
			
			preparedStatement.setInt	(1, kaimasolevaYldkysimuseAndmed.getKaimasolevaKysimuseID());
			preparedStatement.setBoolean(2, kaimasolevaYldkysimuseAndmed.getKasLuuaUuedTiimid());
			preparedStatement.setInt	(3, kaimasolevaYldkysimuseAndmed.getId());
			
			rowsAffected = preparedStatement.executeUpdate();
			
			
			if (rowsAffected == 1)
			{
				logger.info("Update sucessful: " + preparedStatement.toString());
				return rowsAffected;
			}
			else
			{
				logger.error("Update was unsuccesful: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
			return WARNING_NO_ROWS_AFFECTED;		
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}
}
